// Package api
// @Title api[简述该包的主要作用]
// @Description
// @Author  cnHuaShao
// @since v0.0.1
// @Version v1.0.0
// @Date 2024/6/6 17:25
// @Update 2024/6/6 17:25
package api

import (
	"FinancialManagement/src/base/baselog"
	"FinancialManagement/src/base/mess"
	"FinancialManagement/src/base/util"
	"FinancialManagement/src/business/common"
	"FinancialManagement/src/business/sales"
	"github.com/gin-gonic/gin"
	"github.com/shopspring/decimal"
	"go.uber.org/zap"
	"net/http"
	"time"
)

// UpdateSalesInfoOnPurchase
// @Description: 依据采购单信息进行更新销售单中的各项内容
// @param c
func UpdateSalesInfoOnPurchase(c *gin.Context) {
	salesId := util.ToInt(c.DefaultPostForm("salesId", "0")) // 销售单ID
	if salesId == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050111, "请求参数缺失"))
		return
	}
	if err := sales.UpdateSalesInfoOnPurchase(salesId); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050113, "更新销售单失败"))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(""))
}

// DeleteSalesDetailInfo
// @Description: 删除指定销售单
// @param c
func DeleteSalesDetailInfo(c *gin.Context) {
	salesId := util.ToInt(c.DefaultPostForm("salesId", "0")) // 销售单ID
	if salesId == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050111, "请求参数缺失"))
		return
	}
	if err := sales.DeleteSalesDetail(salesId); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050113, "删除销售明细失败"))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(""))
}

// UpdateSalesDetailItem
// @Description: 修改销售明细信息
// @param c
func UpdateSalesDetailItem(c *gin.Context) {
	sdiId := util.ToInt(c.DefaultPostForm("sdiId", "0"))                                   // 销售明细ID
	salesId := util.ToInt(c.DefaultPostForm("salesId", "0"))                               // 销售单ID
	customerId := util.ToInt(c.DefaultPostForm("customerId", "0"))                         // 关联客户ID
	productID := util.ToInt(c.DefaultPostForm("productId", "0"))                           // 关联产品ID
	sdiQuantity, _ := decimal.NewFromString(c.DefaultPostForm("sdiQuantity", "0"))         // 销售数量
	sdiPrice, _ := decimal.NewFromString(c.DefaultPostForm("sdiPrice", "0"))               // 销售单价
	totalPrice, _ := decimal.NewFromString(c.DefaultPostForm("totalPrice", "0"))           // 自动总金额
	sdiActualAmount, _ := decimal.NewFromString(c.DefaultPostForm("sdiActualAmount", "0")) // 实际销售金额
	sdiPureProfit, _ := decimal.NewFromString(c.DefaultPostForm("sdiPureProfit", "0"))

	if salesId == 0 || customerId == 0 || productID == 0 || sdiQuantity.IsZero() || sdiPrice.IsZero() || totalPrice.IsZero() || sdiActualAmount.IsZero() {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050111, "请求参数缺失"))
		return
	}
	sdiInfo := sales.SalesDetailItem{
		SdiID: sdiId,
	}
	err := sdiInfo.GetSalesDetailItem()
	if err != nil {
		baselog.Logger.Error(" 获取销售明细失败", zap.Error(err))
		c.JSON(http.StatusOK, mess.ResErrMess(9100050111, "获取销售明细失败"))
		return
	}

	sdiInfo.SalesID = salesId
	sdiInfo.CustomerID = customerId
	sdiInfo.ProductID = productID
	sdiInfo.SdiQuantity = sdiQuantity
	sdiInfo.SdiPrice = sdiPrice
	sdiInfo.TotalPrice = totalPrice
	sdiInfo.SdiActualAmount = sdiActualAmount
	sdiInfo.SdiPureProfit = sdiPureProfit
	// 修改指定销售明细并更新销售单信息
	if err := sdiInfo.UpdateSalesDetailItem(); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050112, "更新销售明细失败"))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(""))
}

// AddSalesDetailItem
// @Description: 添加销售明细信息
// @param c
func AddSalesDetailItem(c *gin.Context) {
	salesId := util.ToInt(c.DefaultPostForm("salesId", "0"))                                 // 销售单ID
	customerId := util.ToInt(c.DefaultPostForm("customerId", "0"))                           // 关联客户ID
	productID := util.ToInt(c.DefaultPostForm("productId", "0"))                             // 关联产品ID
	sdiQuantity, _ := decimal.NewFromString(c.DefaultPostForm("sdiQuantity", "0"))           // 销售数量
	sdiPrice, _ := decimal.NewFromString(c.DefaultPostForm("sdiPrice", "0"))                 // 销售单价
	totalPrice, _ := decimal.NewFromString(c.DefaultPostForm("totalPrice", "0"))             // 自动总金额
	sdiActualAmount, _ := decimal.NewFromString(c.DefaultPostForm("sdiActualAmount", "0"))   // 实际销售金额
	sdiCostUnitPrice, _ := decimal.NewFromString(c.DefaultPostForm("sdiCostUnitPrice", "0")) // 采购成本单价
	sdiFullCostPrice, _ := decimal.NewFromString(c.DefaultPostForm("sdiFullCostPrice", "0")) // 采购成本总价
	sdiPureProfit, _ := decimal.NewFromString(c.DefaultPostForm("sdiPureProfit", "0"))       // 纯利润
	sdiDate := c.DefaultPostForm("sdiDate", "")                                              // 销售日期
	if productID == 0 || sdiQuantity.IsZero() || sdiPrice.IsZero() || totalPrice.IsZero() || sdiActualAmount.IsZero() || sdiDate == "" {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050111, "请求参数缺失"))
		return
	}

	sdiInfo := sales.SalesDetailItem{
		SalesID:          salesId,
		CustomerID:       customerId,
		ProductID:        productID,
		SdiQuantity:      sdiQuantity,
		SdiPrice:         sdiPrice,
		TotalPrice:       totalPrice,
		SdiActualAmount:  sdiActualAmount,
		SdiCostUnitPrice: sdiCostUnitPrice,
		SdiFullCostPrice: sdiFullCostPrice,
		SdiPureProfit:    sdiPureProfit,
		SdiDate:          common.CustomDate(sdiDate), // 采购出发日期
	}
	err := sdiInfo.CreateSalesDetailItem()
	if err != nil {
		baselog.Logger.Error(" 添加销售明细失败", zap.Error(err))
		c.JSON(http.StatusOK, mess.ResErrMess(9100050111, "添加销售明细失败"))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(""))
}

// UpdateSalesDetail
// @Description: 修改销售订单基本信息
// @param c
func UpdateSalesDetail(c *gin.Context) {
	salesId := util.ToInt(c.DefaultPostForm("salesId", "0"))         // 销售单ID
	salesType := util.ToInt(c.DefaultPostForm("salesType", "0"))     // 销售类型，1正常销售，2全额顶销，3部分顶销
	salesRemarks := c.DefaultPostForm("salesRemarks", "")            // 备注
	salesStatus := util.ToInt(c.DefaultPostForm("salesStatus", "0")) // 订单状态，1暂存，2提交保存，3作废
	salesPerson := c.DefaultPostForm("salesPerson", "")              // 销售人员
	purchaseId := util.ToInt(c.DefaultPostForm("purchaseId", "0"))   // 关联采购ID
	salesCreateTime := time.Now().Unix()                             // 创建时间
	salesInfo := sales.SalesDetail{
		SalesID:         salesId,
		SalesType:       salesType,
		SalesRemarks:    salesRemarks,
		SalesStatus:     salesStatus,
		SalesPerson:     salesPerson,
		PurchaseId:      purchaseId,
		SalesCreateTime: salesCreateTime,
	}
	if err := salesInfo.UpdateSalesDetail; err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050111, "更新销售记录失败"))
		return
	}
}

// CreateSalesDetail
// @Description:   创建销售订单
// @param c
func CreateSalesDetail(c *gin.Context) {
	// salesId := util.ToInt(c.DefaultPostForm("salesId", "0"))         // 销售单ID
	salesType := util.ToInt(c.DefaultPostForm("salesType", "0"))     // 销售类型，1正常销售，2全额顶销，3部分顶销
	salesRemarks := c.DefaultPostForm("salesRemarks", "")            // 备注
	salesStatus := util.ToInt(c.DefaultPostForm("salesStatus", "0")) // 订单状态，1暂存，2提交保存，3作废
	salesPerson := c.DefaultPostForm("salesPerson", "")              // 销售人员
	purchaseId := util.ToInt(c.DefaultPostForm("purchaseId", "0"))   // 关联采购ID
	salesCreateTime := time.Now().Unix()                             // 创建时间
	salesInfo := sales.SalesDetail{
		SalesType:       salesType,
		SalesRemarks:    salesRemarks,
		SalesStatus:     salesStatus,
		SalesPerson:     salesPerson,
		PurchaseId:      purchaseId,
		SalesCreateTime: salesCreateTime,
	}

	if err := salesInfo.CreateSalesDetail(); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050110, "创建销售记录失败"))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(""))
}

// DeleteSalesDetailItemInfo
// @Description: 删除具体销售明细信息
// @param c
func DeleteSalesDetailItemInfo(c *gin.Context) {
	sdiId := util.ToInt(c.DefaultPostForm("sdiId", "0"))     // 删除销售明细ID
	salesId := util.ToInt(c.DefaultPostForm("salesId", "0")) // 所属销售ID
	if salesId == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(910005001, "请求参数不能为空"))
		return
	}
	if err := sales.DeleteSalesDetailItem(sdiId, salesId); err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(9100050112, "删除该销售记录失败"))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(""))
}

// GetSalesDetailInfo
// @Description: 获取指定销售信息详情
// @param c
func GetSalesDetailInfo(c *gin.Context) {
	salesId := util.ToInt(c.DefaultPostForm("salesId", "0"))
	if salesId == 0 {
		c.JSON(http.StatusOK, mess.ResErrMess(910005001, "请求参数不能为空"))
		return
	}
	salesDetailInfo, err := sales.GetSalesDetailInfoAndItems(salesId)
	if err != nil {
		baselog.Logger.Error("错误", zap.Error(err))
		c.JSON(http.StatusOK, mess.ResErrMess(9100050111, "获取该销售记录详情失败"))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(salesDetailInfo))
}

// GetSalesDetailList
// @Description: 获取销售信息列表
// @param c
func GetSalesDetailList(c *gin.Context) {
	pageSize := util.ToInt(c.DefaultPostForm("pageSize", "10"))    // 显示行数
	pageNumber := util.ToInt(c.DefaultPostForm("pageNumber", "1")) // 当前页
	salesType := util.ToInt(c.DefaultPostForm("salesType", "0"))   // 销售类型，1正常销售，2全额顶销，3部分顶销
	sd := sales.SalesDetail{
		SalesType: salesType,
	}
	salesDetailList, count, err := sd.GetSalesDetailList(pageSize, pageNumber)
	if err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(9005011, "获取销售信息列表失败"))
		return
	}
	c.JSON(http.StatusOK, mess.ResMessOK(gin.H{
		"list":  salesDetailList,
		"count": count,
	}))
}
